﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Timer
{
    /// <summary>
    ///     提供了对于用户心跳包的相关超时计算和操作
    /// </summary>
    class HeartBreathTimeOutEvent : TimeOutEvent
    {
        /// <summary>
        ///     提供了对于用户心跳包的相关超时计算和操作
        /// </summary>
        /// <param name="Key">唯一标示</param>
        /// <param name="Interval">触发间隔</param>
        public HeartBreathTimeOutEvent(String Key, int Interval)
            : base(Key, Interval, -1)
        {
            this.OnTick += new EventHandler<EventArgs>(HreatBreathTimeOutEvent_OnTick);
        }

        void HreatBreathTimeOutEvent_OnTick(object sender, EventArgs e)
        {
            //得到用户
            User.User user = Globle.userManager.GetUser(this.Key);
            //得到用户基础会话
            Session.Session session = Globle.sessionManager.GetMemberByKey(this.Key) as Session.Session;
            if(user != null && session !=null)
            {
                //废除会话
                session.Exprid = true;
                Globle.sessionManager.Remove(session);
                user.SetOffline(session);
            }
            this.timer.Close();
            //移除对于该用户的心跳计算
            Globle.timeoutTimerManager.RemoveTimer(this);
        }
    }
}
